﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports log4net
Imports Equipment.DataAccess

Partial Public Class AssetCustomField
    Private Shared logger As ILog = LogManager.GetLogger(GetType(AssetCustomField))

#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property
#End Region

#Region "Private Section"
    Private Shared Sub SetDBContextInColection(ByRef list As IQueryable(Of AssetCustomField), ByRef context As EquipmentDataContext)
        For Each item As AssetCustomField In list
            item.DBContext = context
        Next
    End Sub
#End Region

#Region "Public Section"
    Public Shared Function RetrieveAllByType(ByVal type As CustomFieldType) As IEnumerable(Of AssetCustomField)
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From cf In Database.Instance.AssetCustomFields Where cf.FieldType = type
        SetDBContextInColection(result, db)
        Return result
    End Function

    Public Shared Function RetrieveAllActiveByType(ByVal type As CustomFieldType) As IEnumerable(Of AssetCustomField)
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From cf In Database.Instance.AssetCustomFields Where cf.FieldType = type AndAlso cf.IsActive = True
        SetDBContextInColection(result, db)
        Return result
    End Function

    Public Shared Function GetFirst(ByVal sequenceID As Integer, ByVal type As CustomFieldType) As AssetCustomField
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From f In db.AssetCustomFields Where f.SequenceID = sequenceID AndAlso f.FieldType = type
        Dim fieldEntity As AssetCustomField = result.FirstOrDefault()

        If fieldEntity IsNot Nothing Then
            fieldEntity.DBContext = db
        End If

        Return fieldEntity
    End Function

    Public Sub Save()
        If Me.DBContext IsNot Nothing Then
            Dim _dataBase As EquipmentDataContext = Me.DBContext

            Try
                _dataBase.SubmitChanges()
            Catch ex As Exception
                logger.Error(ex.Message + ":" + ex.StackTrace)
            End Try
        End If
    End Sub

    Public Shared Sub Insert(ByVal fieldToAdd As AssetCustomField)

        Dim _dataBase As EquipmentDataContext = Database.Instance

        _dataBase.AssetCustomFields.InsertOnSubmit(fieldToAdd)

        Try
            _dataBase.SubmitChanges()

        Catch ex As Exception
            logger.Error(ex.Message + ":" + ex.StackTrace)
        End Try

    End Sub


    Public Shared Sub Update(ByVal newField As AssetCustomField)

        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim fieldQuery = From f In _dataBase.AssetCustomFields
        Dim fieldToUpdate As AssetCustomField = fieldQuery.SingleOrDefault(Function(f) f.ID = newField.ID)

        If fieldToUpdate IsNot Nothing Then

            fieldToUpdate.FieldName = newField.FieldName
            fieldToUpdate.IsActive = newField.IsActive
            fieldToUpdate.Mandatory = newField.Mandatory
            fieldToUpdate.Values = newField.Values
            fieldToUpdate.SequenceID = newField.SequenceID
            fieldToUpdate.FieldType = newField.FieldType

        End If

        Try
            _dataBase.SubmitChanges()
        Catch ex As Exception
            logger.Error(ex.Message + ":" + ex.StackTrace)
        End Try
    End Sub

#End Region

End Class
